package cn.ifavor.appframework.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.ifavor.appframework.R;
import cn.ifavor.appframework.lifecycle.bean.User;

/**
 * @author: SvenHe(heshiweij@gmail.com)
 * @Date: 2016-06-05
 * @Time: 15:35
 * @des ${TODO}
 */
public class RecyclerActivity extends BaseRecyclerActivity<User> {

    /**
     * 初始化内容视图
     */
    protected void setUpContentView() {
        setContentView(R.layout.activity_recycle, R.string.app_name, R.mipmap.ic_launcher);
    }

    @Override
    protected void setUpView() {
        super.setUpView();


    }

    @Override
    protected List<User> setUpData() {
        mDatas = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            mDatas.add( new User("name:" + i, i) );
        }
        return mDatas;
    }

    @Override
    public void onRefresh() {
        for (int i = 0; i < 2; i++) {
            mDatas.add( new User("name:" + i, i) );
        }
        mSwipeRefreshLayout.setRefreshing(false);
        mListAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onBind(RecyclerView.ViewHolder holder, int position) {
        ((MyViewHolder)holder).setData(mDatas.get(position));
    }

    @Override
    protected RecyclerView.ViewHolder getViewHolder(ViewGroup parent, int viewType) {
        View viewItem = View.inflate(this, R.layout.item_layout, null);
        return new  MyViewHolder(viewItem);
    }

    public class MyViewHolder extends  RecyclerView.ViewHolder{

        private final TextView mTvItem;

        /* 初始化view、控件 */
        public MyViewHolder(View itemView) {
            super(itemView);

            mTvItem = (TextView) itemView.findViewById(R.id.tv_item);
        }

        /* 设置数据 */
        public void setData(User user) {
            mTvItem.setText(user.getName());

        }
    }
}
